'use strict';

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');

function request(requestData) {
	// 解析过滤条件
	let filters = filter(requestData.condition);

	// 作业数
	let workData = {
		// 主体目标
		'data': {
			'target': 'homework-homework_id',
			'data_type': 'count',
			'type': 'normal'
		},
		// 时间粒度
		'time_unit': '',
		// 分组显示标识
		'multi_unit': 'teacher-province_id',
		// 筛选条件
		'filter': filters
	};

	// 题目数
	let subjectData = {
		// 主体目标
		'data': {
			'target': 'homework-count',
			'data_type': 'sum',
			'type': 'normal'
		},
		// 时间粒度
		'time_unit': '',
		// 分组显示标识
		'multi_unit': 'teacher-province_id',
		// 筛选条件
		'filter': filters
	};

	// 教师数
	let teacherData = {
		// 主体目标
		'data': {
			'target': 'homework-teacher_id',
			'data_type': 'count',
			'type': 'normal'
		},
		// 时间粒度
		'time_unit': '',
		// 分组显示标识
		'multi_unit': 'teacher-province_id',
		// 筛选条件
		'filter': filters
	};

	return [workData, teacherData, subjectData];
}

function response(workData, teacherData, subjectData) {
	let rn = {
		series: [],
		categories: new Set()
	};

	let data = [{
		title: '作业数',
		content: workData || []
	}, {
		title: '教师数',
		content: teacherData || []
	}, {
		title: '题目数',
		content: subjectData || []
	}];

	rn = data.reduce((rn, item) => {
		let arr = [];

		item.content.forEach((item) => {
			rn.categories.add(item.show_name || item.time_sheet);
			arr.push(parseFloat(item.result));
		});

		rn.series.push({
			name: item.title,
			data: arr
		});

		return rn;
	}, rn);

	rn.categories = Array.from(rn.categories);

	return rn;
}

function filter(requestData) {
	let filters = [];
	let condition = requestData;

	if (condition.province && condition.province != builder.NIL) {
		filters.push(builder.append('teacher-province_id', '=', condition.province));
	}
	if (condition.city && condition.city != builder.NIL) {
		filters.push(builder.append('teacher-city_id', '=', condition.city));
	}
	if (condition.county && condition.county != builder.NIL) {
		filters.push(builder.append('teacher-county_id', '=', condition.county));
	}
	if (condition.school && condition.school != builder.NIL) {
		filters.push(builder.append('teacher-school_id', '=', condition.school));
	}
	if (condition.stage[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade_part', 'in', condition.stage.join(',')));
	}
	if (condition.grade[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade', 'in', condition.grade.join(',')));
	}
	if (condition.subject[0] != builder.NIL) {
		filters.push(builder.append('teacher-subject', 'in', condition.subject.join(',')));
	}
	if (condition.version != '0') {
		filters.push(builder.append('teacher-version', '=', condition.version));
	}
	if (condition.deviceType[0] != builder.NIL) {
		filters.push(builder.append('teacher-source', 'in', condition.deviceType.join(',')));
	}

	filters.push(builder.append('homework-day', '>=', condition.startDate));
	filters.push(builder.append('homework-day', '<=', condition.endDate));
	filters.push(builder.append('teacher-student_count', '>=', condition.userFeature));
	// filters.push(builder.append('teacher-student_count', '<=', condition.userFeature.end));

	return filters;
}

module.exports = {
	API_URL: api.API_PACKAGE_DATA,
	request,
	response
}